package com.hondee.fuzzyquery.entity.access.querycondition;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SqlWhereStringBuilder {
	
	private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static String build(String field,SqlWhereConditionEnum whereCondition,Object param1,Object param2){
		if(whereCondition==null){
			return "";
		}
		String target = null;
		switch(whereCondition){
			case CONTAIN : target = contains(field,param1);break;
			case DATE_EQUAL:target = dateEquals(field,param1);break;
			case EQUALS:target = equals(field,param1);break;
			case GREATER:target = greater(field,param1);break;
			case GREATER_EQUALS:target = greaterEquals(field,param1);break;
			case IN:target = in(field,param1);break;
			case IS_NULL :target = isnull(field);break;
			case ISNOT_NULL:target = isnotnull(field);break;
			case LESS:target = less(field,param1);break;
			case LESS_EQUALS:target = lessEquals(field,param1);break;
			case NOT_IN:target = notin(field,param1);break;
		}	
		return target==null?"":target;
	}
	
	public static String contains(String field,Object param1){
		if(param1==null){
			return null;
		}
		return field + " LIKE '%"	+ param1	+ "%'";
	}
	public static String dateEquals(String field,Object param1){
		String returnStr = null;
		if(param1 == null){
			return returnStr;
		}
		try{
			String value = param1.toString();
			if(value.length() > 0){
				value = value.replaceAll("T", " ");
				Date currentDate = df.parse(value);
				Date tomorrow = null;
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(currentDate);
				calendar.add(Calendar.DATE, 1);
				tomorrow = calendar.getTime();
				String tomorrowStr = df.format(tomorrow);
				return field+" >= '"+value+"' AND "+field+" < '"+tomorrowStr+"'";
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		};
		return returnStr;
	}
	public static String equals(String field,Object param1){
		if(param1==null){
			return null;
		}
		return field + "='" + param1 + "'";
	}
	public static String greater(String field,Object param1){
		if(param1==null){
			return null;
		}
		return field+">'"+param1+"'";
	}
	public static String greaterEquals(String field,Object param1){
		if(param1==null){
			return null;
		}
		return field+">='"+param1+"'";
	}
	public static String in(String field,Object param1){
		if(param1==null){
			return null;
		}
		return field+" IN("+param1+")";
	}
	public static String isnull(String field){
		return field+" IS NULL";
	}
	public static String isnotnull(String field){
		return field+" IS NOT NULL";
	}
	public static String less(String field,Object param1){
		if(param1==null){
			return null;
		}
		return field+" < '"+param1+"'";
	}
	public static String lessEquals(String field,Object param1){
		if(param1==null){
			return null;
		}
		return field+" <= '"+param1+"'";
	}
	public static String notin(String field,Object param1){
		if(param1==null){
			return null;
		}
		return field+" NOT IN("+param1+")";
	}
}
